package com.ozan.kemal.db;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.ozan.kemal.model.User;
import com.ozan.kemal.utils.Log;


public class UserMgrDB extends DBManager{

	private static UserMgrDB instance=null;
	private static String TABLE_NAME="users";

	public static UserMgrDB getInstance(){
		if(instance==null){
			instance=new UserMgrDB();
		}

		return instance;
	}


	private UserMgrDB() {

		super();		
	}


	public User setUser(ResultSet rs) throws SQLException{

		User u = null;
		if (rs.next()) { 
			u = new User() ;
			u.setId(rs.getInt("userId")); 
			u.setUserName(rs.getString("userName")); 
			u.setPassword(rs.getString("password")); 
			u.setEmail(rs.getString("email")); 
			u.setLoginCount(rs.getInt("loginCount")); 
			u.setDescription(rs.getString("description")); 
			u.setLastLoginDate(rs.getTimestamp("lastLoginDate")); 
			u.setRegisteredDate(rs.getTimestamp("registeredDate")); 

			Log.add(u.getUserName()+" logged in");

		} 

		return u ;

	}

	private  String ADD_USER_SQL="INSERT INTO "+TABLE_NAME+" (" +
			" userName, password, email, loginCount, description, lastLoginDate, registeredDate) " +
			" VALUES(?, ?, ?, ?, ?, ?, ?)";

	public void addNew(User u) throws Exception {

		if (ds != null) {
			Connection conn = null;
			try {

				conn = ds.getConnection();

				pst = conn.prepareStatement(ADD_USER_SQL);

				pst.setString(1, u.getUserName());
				pst.setString(2, u.getPassword());
				pst.setString(3, u.getEmail());
				pst.setInt(4, u.getLoginCount());
				pst.setString(5,u.getDescription());
				pst.setTimestamp(6,u.getLastLoginDate());
				pst.setTimestamp(7,u.getRegisteredDate());

				pst.executeUpdate();

				Log.add( u.getUserName()+" eklendi");

			} catch (SQLException e) {
				Log.add(e.getMessage());
				throw new Exception(e);

			}finally {

				try {
					if (pst != null) {
						pst.close();
					}
					if (conn != null) {
						conn.close();
					}

				} catch (SQLException ex) {
					Log.add(ex.getMessage());
				}

			}
		}


	}




	private  String GET_USER_SQL="SELECT * FROM "+TABLE_NAME+" WHERE userName=? AND password = ? ";

	public User findUser(String userName,String password) throws Exception{
		User u= null;
		ResultSet rs=null;

		if (ds != null) {
			Connection conn = null;
			try {

				conn = ds.getConnection();

				pst = conn.prepareStatement(GET_USER_SQL);
				pst.setString(1,userName);
				pst.setString(2,password);
				rs = pst.executeQuery();

				if(rs != null  ){
					u=setUser(rs);
				}

			} catch (SQLException e) {
				Log.add(e.getMessage());
				throw new Exception(e);

			}finally {

				try {
					if (rs != null) {
						rs.close();
					}
					if (pst != null) {
						pst.close();
					}
					if (conn != null) {
						conn.close();
					}

				} catch (SQLException ex) {
					Log.add(ex.getMessage());
				}
			}
		}

		return u ;

	}



	private  String UPDATE_USER_SQL="UPDATE "+TABLE_NAME+" SET  lastLoginDate = ? WHERE userName = ? ";

	public void setLastLoginDate(User u) throws Exception{


		if (ds != null) {
			Connection conn = null;
			try {

				conn = ds.getConnection();

				pst = conn.prepareStatement(UPDATE_USER_SQL);

				pst.setTimestamp(1, u.getLastLoginDate() );
				pst.setString(2, u.getUserName());

				pst.executeUpdate();

			} catch (SQLException e) {
				Log.add(e.getMessage());
				throw new Exception(e);

			}finally {

				try {
					if (pst != null) {
						pst.close();
					}
					if (conn != null) {
						conn.close();
					}

				} catch (SQLException ex) {
					Log.add(ex.getMessage());
				}

			}
		}





	}


}
